<?php
//	$url="localhost";
//	$user_name="root";
//	$db_password="password";
//	$db_name="adaptive_learning";
//	$con=mysqli_connect($url, $user_name, $db_password, $db_name);
	include 'db_connect.php';


	$s_id = $_GET["matric"];
	$module = $_GET["module"];
	$topic = $_GET["topic"];
	$question = $_GET["question"];
	$answer = $_GET["answer"];
	$D = $_GET["D"];
	$L = $_GET["L"];
	$H = $_GET["H"];
	$R = $_GET["R"];
	$B = 0;
	$S = 0;
	$T = 0;

	// Check connection
	if ($con) {
		// get resule
		$result = mysqli_query($con,"SELECT answer FROM Question WHERE m_code = '" . $module . "' AND t_id = " . $topic . " AND id = " . $question);
		$row = mysqli_fetch_array($result);
		$L = $L + 1;
		$H = $H + $D;

		$lastResponseResult;
		if ($row['answer'] == $answer) {
			if ($L <= 4) { // run Wright algorithm for first 4 questions
				$D = $D + 1/$L;
			} else { // linear increasing in subsequence questions
				$D = $D + 0.3;
			}
			$R = $R + 1;
			$lastResponseResult=true;
		} else {
			if ($L <= 4) { // run Wright algorithm for first 4 questions
				$D = $D - 1/$L;
			} else { // linear decreasing in subsequence questions
				$D = $D - 0.15;
			}
			$lastResponseResult=false;
		}

		$W = $L - $R;
		if ($W != 0) {
			$B = $H/$L + log($R/$W, 10);
			$S = ($L/($R*$W));
		} else {
			$B = $H/$L;
			$S = 0;
		}

		$status = "training";
		if ($L > 5) {
			$result = mysqli_query($con,"SELECT pass_standard FROM Topic WHERE m_code = '" . $module . "' AND id = " . $topic);
			$row = mysqli_fetch_array($result);
			$T = (double) $row['pass_standard'];

			if ($T < ($B - $S)) {
				$status = "pass";
			} else {
				$status = "not_qualified";
			}
		}

		$response = array( "status" => $status, "D" => $D, "L" => $L, "H" => $H, "R" => $R, "B" => $B, "S" => $S, "T" => $T, "lastResponseResult" => $lastResponseResult);

		echo json_encode($response);
	}
?>